//-----------------------------------------------------------------------
// <copyright file="AddMimeType.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Adds an Http Header to a web site</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Adds a new Mime Type to the specified web site
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.AddMimeType 
    ///     Identifier="identifier"
    ///     Description="description"
    ///     FileExtension="FileExtension"
    ///     MimeType="MimeType" />]]></code>
    /// <para>where:</para>
    /// <para><i>description</i></para>
    /// <para>Web site description to add the Mime type to</para>
    /// <para><i>identifier</i></para>
    /// <para>optional parameter to identify website when 'description' is not used</para>
    /// <para><i>FileExtension (Required)</i></para>
    /// <para>File extension for the new type</para>
    /// <para><i>MimeType (Required)</i></para>
    /// <para>Mime type string</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to operate on(defaults to localhost)</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.AddMimeType
    ///             Desciption="mytestsite"
    ///             FileExtension=".flv"
    ///             MimeType="video/x-flv"/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class AddMimeType : TaskBase
    {
        private string description;
        private string identifier;
        private string fileExtension;
        private string mimeType;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// File extension for mime type
        /// </summary>
        /// <value>extension</value>
        /// <remarks>required</remarks>
        [Required]
        public string FileExtension
        {
            get { return (this.fileExtension ?? String.Empty); }
            set { this.fileExtension = value; }
        }

        /// <summary>
        /// String describing MimeType
        /// </summary>
        /// <value>type</value>
        /// <remarks>required</remarks>
        [Required]
        public string MimeType
        {
            get { return (this.mimeType ?? String.Empty); }
            set { this.mimeType = value; }
        }

        /// <summary>
        /// Description of web site to which we are adding the filter
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to apply the binding to</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier for website to which we are adding the filter
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// </remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }
            set { this.identifier = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.Description, this.Identifier, "AddMimeType", this.MachineName);
            if (WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite web = WebSite.Load(this.MachineName, id);
                Microsoft.Sdc.Tasks.Configuration.Web.MimeTypes mimeTypes = new Microsoft.Sdc.Tasks.Configuration.Web.MimeTypes(web);
                mimeTypes.Add(this.FileExtension, this.MimeType);
                web.Save();
            }
        }
    }
}